package com.youlai.boot.factory.controller;

import com.youlai.boot.factory.service.SettlementItemsService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.factory.model.form.SettlementItemsForm;
import com.youlai.boot.factory.model.query.SettlementItemsQuery;
import com.youlai.boot.factory.model.vo.SettlementItemsVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

/**
 * 结算单明细前端控制层
 *
 * @author youlaitech
 * @since 2025-07-06 15:40
 */
@Tag(name = "结算单明细接口")
@RestController
@RequestMapping("/api/v1/settlement-items")
@RequiredArgsConstructor
public class SettlementItemsController  {

    private final SettlementItemsService settlementItemsService;

    @Operation(summary = "结算单明细分页列表")
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPerm('factory:settlement-items:query')")
    public PageResult<SettlementItemsVO> getSettlementItemsPage(SettlementItemsQuery queryParams ) {
        IPage<SettlementItemsVO> result = settlementItemsService.getSettlementItemsPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "新增结算单明细")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('factory:settlement-items:add')")
    public Result<Void> saveSettlementItems(@RequestBody @Valid SettlementItemsForm formData ) {
        boolean result = settlementItemsService.saveSettlementItems(formData);
        return Result.judge(result);
    }

    @Operation(summary = "获取结算单明细表单数据")
    @GetMapping("/{id}/form")
    @PreAuthorize("@ss.hasPerm('factory:settlement-items:edit')")
    public Result<SettlementItemsForm> getSettlementItemsForm(
        @Parameter(description = "结算单明细ID") @PathVariable Long id
    ) {
        SettlementItemsForm formData = settlementItemsService.getSettlementItemsFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "修改结算单明细")
    @PutMapping(value = "/{id}")
    @PreAuthorize("@ss.hasPerm('factory:settlement-items:edit')")
    public Result<Void> updateSettlementItems(
            @Parameter(description = "结算单明细ID") @PathVariable Long id,
            @RequestBody @Validated SettlementItemsForm formData
    ) {
        boolean result = settlementItemsService.updateSettlementItems(id, formData);
        return Result.judge(result);
    }

    @Operation(summary = "删除结算单明细")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('factory:settlement-items:delete')")
    public Result<Void> deleteSettlementItemss(
        @Parameter(description = "结算单明细ID，多个以英文逗号(,)分割") @PathVariable String ids
    ) {
        boolean result = settlementItemsService.deleteSettlementItemss(ids);
        return Result.judge(result);
    }
}
